#!/usr/bin/env python
# encoding: utf-8
"""
forms.py

Created by reedhong on 2011-10-18.
Copyright (c) 2011 __MyCompanyName__. All rights reserved.
"""

#coding=utf-8

from django import forms
from report.models import Report, ReportReason
from django.contrib.contenttypes.models import ContentType

    
class ReportForm(forms.Form):
    reason = forms.ModelChoiceField(label=u"原因", queryset=ReportReason.objects.all())
    
    warn = ""
        
    def __init__(self, request=None, *args, **kwargs):
        super(ReportForm, self).__init__(*args, **kwargs)
        
    def save(self, user, target):
        content_type = ContentType.objects.get_for_model(target)
        reason = self.cleaned_data.get("reason", None)
        try:
            report,created = Report.objects.get_or_create(content_type=content_type, object_pk = target.pk, user=user)
            if not created:
                report.times += 1   
            report.report_reason = reason
            report.save()  
        except Report.MultipleObjectsReturned:
            pass